package com.rr.mymoney.dao.impl.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.rr.mymoney.dao.UserDao;
import com.rr.mymoney.model.User;

@Repository
public class UserDaoImpl extends AbstractDao implements UserDao {
    public void saveOrUpdate(User user) {
	getCurrentSession().saveOrUpdate(user);
	getCurrentSession().flush();
    }

    public User get(Long id) {
	return (User) getCurrentSession().get(User.class, id);
    }

    public void delete(User user) {
	getCurrentSession().delete(user);
    }

    @SuppressWarnings("unchecked")
    public List<User> getAll() {
	return find("from User");
    }

    public User get(String username) {
	@SuppressWarnings("unchecked")
	List<User> list = find("from User where username = ?", username);
	if (list.size() > 0) {
	    return list.get(0);
	} else {
	    return null;
	}
    }
}
